package com.dpiinc.TechPage.pen.fragment

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.dpiinc.TechPage.R
import com.dpiinc.TechPage.activity.EditNoteActivity
import com.beiji.lib.pen.PenManager
import com.beiji.lib.pen.model.DotUnit
import com.beiji.lib.pen.model.PenInfo
import kotlinx.android.synthetic.main.fragment_test_connection.*

/**
 * 书写测试Fragment
 * Created by X on 2018/6/14.
 */
class TestConnectionFragment : Fragment() {

    private var penInfo: PenInfo? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        penInfo = arguments?.getSerializable(ARG_PEN_INFO) as PenInfo?
        PenManager.instance.registerDataCallback(penCallback)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_test_connection, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        tv_pen_name.text = penInfo?.name
        tv_mac_address.text = penInfo?.macAddress
    }

    private val penCallback: (DotUnit) -> Boolean = {
        PenManager.instance.cacheDotTemporary(it)
        activity?.let {
            startActivity(Intent(it, EditNoteActivity::class.java))
            it.finish()
        }
        true
    }

    override fun onDestroy() {
        PenManager.instance.unRegisterDataCallback(penCallback)
        super.onDestroy()
    }

    companion object {
        private const val ARG_PEN_INFO = "pen_info"
        private val TAG = TestConnectionFragment::class.java.simpleName

        fun newInstance(pen: PenInfo?): TestConnectionFragment {
            val fragment = TestConnectionFragment()
            val args = Bundle()
            args.putSerializable(ARG_PEN_INFO, pen)
            fragment.arguments = args
            return fragment
        }
    }
}// Required empty public constructor
